package com.hangzhou.xyj.ui.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.utils.StringUtil;

/**
 * Created by DT-038 on 2015/9/2.
 * 顶部公共标题栏title bar
 */
public class CommonTitleBarView extends LinearLayout implements View.OnClickListener {

    //左导航
    private RelativeLayout mRlBackRoot;
    private TextView mTvBack;
    private ImageView mIvBack;

    //标题
    private RelativeLayout mRlTitleRoot;
    private TextView mTvTitle;
    private RelativeLayout mRlTitleContainer;
    private View mTitleView;    //外部传进来的自定义View

    //右导航
    private LinearLayout mRlRightRoot;
    private TextView mTvRight;
    private ImageView mIvRight;
    private ImageView mIvRight2;//第二图
    private LinearLayout mLlImg1;//第一图区域
    private LinearLayout mLlImg2;//第二图区域

    private OnBackListener mNavLeftListener;
    private OnNavRightListener mNavRightListener;
    private OnTitleListener mTitleListener;
    private OnNavRightLeftImgListener mNavRightLeftImgListener;//左边的图片第一个图的监听器

    /**
     * 提供外部自定义的titleView
     *
     * @return
     */
    public View getTitleView() {
        return mTitleView;
    }

    public CommonTitleBarView(Context context) {
        this(context, null);
    }

    public CommonTitleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initListener();
    }

    /**
     * 初始化布局
     *
     * @param context
     */
    private void initView(Context context) {
        //设置垂直方向
        setOrientation(VERTICAL);
        //设置默认背景色
        setBackgroundResource(R.color.common_purple);
        View.inflate(context, R.layout.layout_top_bar, this);

        //左导航
        mRlBackRoot = (RelativeLayout) findViewById(R.id.rl_top_bar_back_root);
        mTvBack = (TextView) findViewById(R.id.tv_top_bar_back);
        mIvBack = (ImageView) findViewById(R.id.iv_top_bar_back);

        //标题
        mRlTitleRoot = (RelativeLayout) findViewById(R.id.rl_top_bar_title_root);
        mTvTitle = (TextView) findViewById(R.id.tv_top_bar_title);
        mRlTitleContainer = (RelativeLayout) findViewById(R.id.rl_top_bar_title_container);

        //右导航
        mRlRightRoot = (LinearLayout) findViewById(R.id.rl_top_bar_right_root);
        mTvRight = (TextView) findViewById(R.id.tv_top_bar_right);
        mIvRight = (ImageView) findViewById(R.id.iv_top_bar_right);
        mIvRight2 = (ImageView) findViewById(R.id.iv_top_bar_right_img2);
        mLlImg1 = (LinearLayout) findViewById(R.id.ll_img1);
        mLlImg2 = (LinearLayout) findViewById(R.id.ll_img2);

        //设置默认状态
        enableTop(true);
        enableBack(true, R.drawable.ic_return_arrow);
        enableRightNav(false);
    }

    /**
     * 设置监听器
     */
    private void initListener() {
        mRlBackRoot.setOnClickListener(this);
        mTvTitle.setOnClickListener(this);
//        mRlRightRoot.setOnClickListener(this);
        mLlImg1.setOnClickListener(this);
        mLlImg2.setOnClickListener(this);
        mTvRight.setOnClickListener(this);
    }

    /**
     * 是否显示顶部导航
     *
     * @param enabled
     */
    public void enableTop(boolean enabled) {
        this.setVisibility(enabled ? View.VISIBLE : View.GONE);
    }


    /**
     * 返回按钮是否可用
     *
     * @param enabled
     */
    public void enableBack(boolean enabled) {
        mRlBackRoot.setVisibility(enabled ? View.VISIBLE : View.GONE);
    }

    /**
     * 返回按钮是否可用
     *
     * @param enabled 是否可用
     * @param resId   资源id
     */
    public void enableBack(boolean enabled, int resId) {
        String typeName = getResources().getResourceTypeName(resId);
        if ("drawable".equals(typeName)) {
            enableBack(enabled, getResources().getDrawable(resId));
        } else if ("string".equals(typeName)) {
            enableBack(enabled, getResources().getString(resId));
        }
    }

    public void enableBack(boolean enabled, View view) {
        enableBack(enabled);
        if (enabled) {
            mRlBackRoot.removeAllViews();
            mRlBackRoot.addView(view);
        }
    }

    /**
     * 返回键显示图片
     *
     * @param enabled
     * @param drawable
     */
    public void enableBack(boolean enabled, Drawable drawable) {
        if (enabled) {
            mIvBack.setImageDrawable(drawable);
            mIvBack.setVisibility(View.VISIBLE);
            mTvBack.setVisibility(View.GONE);
        }
        enableBack(enabled);
    }

    /**
     * 返回键显示文本
     *
     * @param enabled
     * @param backNav
     */
    public void enableBack(boolean enabled, String backNav) {
        if (enabled) {
            mTvBack.setText(backNav + "");
            mTvBack.setVisibility(View.VISIBLE);
            mIvBack.setVisibility(View.GONE);
        }
        enableBack(enabled);
    }

    public void enableTitle(boolean enabled) {
        mRlTitleRoot.setVisibility(enabled ? View.VISIBLE : View.GONE);
    }

    public void enableTitle(boolean enabled, String title) {
        if (enabled) {
            title = StringUtil.nullToEmpty(title);
            if (title.length() > 6) {
                title = title.substring(0, 6) + "...";
            }
            mTvTitle.setText(title);
            mTvTitle.setVisibility(View.VISIBLE);
            mRlTitleContainer.setVisibility(View.GONE);
        }
        enableTitle(enabled);
    }

    public void enableTitle(boolean enabled, int resId) {
        if (enabled) {
            String typeName = getResources().getResourceTypeName(resId);
            if ("layout".equals(typeName)) {
                mTitleView = View.inflate(getContext(), resId, null);
                enableTitle(enabled, mTitleView);
            }
        } else {
            enableTitle(enabled);
        }
    }

    /**
     * 自定义显示标题布局
     *
     * @param enabled
     * @param titleView
     */
    public void enableTitle(boolean enabled, View titleView) {
        if (enabled) {
            this.mTitleView = titleView;
            mTvTitle.setVisibility(View.GONE);
            mRlTitleContainer.setVisibility(View.VISIBLE);
            mRlTitleContainer.removeAllViews();
            mRlTitleContainer.addView(titleView);
        }
        enableTitle(enabled);
    }

    public void enableRightNav(boolean enabled) {
        mRlRightRoot.setVisibility(enabled ? View.VISIBLE : View.GONE);
    }

    public void enableRightNav(boolean enabled, int resId) {
        String typeName = getResources().getResourceTypeName(resId);
        if ("drawable".equals(typeName)) {
            enableRightNav(enabled, getResources().getDrawable(resId));
        } else if ("string".equals(typeName)) {
            enableRightNav(enabled, getResources().getString(resId));
        }
    }

    /**
     * 有2张图片的情况
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/4/22 10:58
     */
    public void enableRightNavTwoImg(boolean enabled, Drawable img1Drawable, Drawable img2Drawable) {
        if (enabled) {
            mIvRight.setImageDrawable(img1Drawable);
            mIvRight2.setImageDrawable(img2Drawable);
            mLlImg1.setVisibility(View.VISIBLE);
            mLlImg2.setVisibility(VISIBLE);
            mTvRight.setVisibility(View.GONE);
        }
        enableRightNav(enabled);
    }

    public void enableRightNav(boolean enabled, Drawable drawable) {
        if (enabled) {
            mIvRight2.setImageDrawable(drawable);
            mLlImg2.setVisibility(View.VISIBLE);
            mTvRight.setVisibility(View.GONE);
        }
        enableRightNav(enabled);
    }

    public void enableRightNav(boolean enabled, String rightNav) {
        if (enabled) {
            mTvRight.setText(rightNav + "");
            mTvRight.setVisibility(View.VISIBLE);
//            mIvRight.setVisibility(View.GONE);
        }
        enableRightNav(enabled);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_top_bar_back_root:
                navLeft();
                break;
            case R.id.tv_top_bar_title:
                title();
                break;
            case R.id.ll_img1:
                mNavRightLeftImgListener.OnNavRightLeftImg();
                break;
            case R.id.ll_img2:
                navRight();
                break;
            case R.id.tv_top_bar_right:
                navRight();
        }
    }


    /**
     * 设置左导航事件
     *
     * @param listener
     */
    public void setOnBackListener(OnBackListener listener) {
        this.mNavLeftListener = listener;
    }

    /**
     * 设置右导航事件
     *
     * @param listener
     */
    public void setOnNavRightListener(OnNavRightListener listener) {
        this.mNavRightListener = listener;
    }

    public void setOnNavRightLeftImgListener(OnNavRightLeftImgListener listener) {
        this.mNavRightLeftImgListener = listener;
    }

    /**
     * 设置标题事件
     *
     * @param listener
     */
    public void setOnTitleListener(OnTitleListener listener) {
        this.mTitleListener = listener;
    }

    /**
     * 执行左导航事件
     */
    private void navLeft() {
        if (mNavLeftListener != null) {
            mNavLeftListener.onBack();
        }
    }

    /**
     * 执行右导航事件
     */
    private void navRight() {
        if (mNavRightListener != null) {
            mNavRightListener.onNavRight();
        }
    }

    /**
     * 执行点击标题事件
     */
    private void title() {
        if (mTitleListener != null) {
            mTitleListener.onTitle();
        }
    }

    /**
     * 左导航返回点击监听器
     */
    public interface OnBackListener {
        public void onBack();
    }

    /**
     * 有导航点击监听器
     */
    public interface OnNavRightListener {
        public void onNavRight();
    }

    /**
     * 右导航有2个图的时候 左图的监听器
     */
    public interface OnNavRightLeftImgListener {
        void OnNavRightLeftImg();
    }

    /**
     * 标题点击监听器
     */
    public interface OnTitleListener {
        public void onTitle();
    }
}
